﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LV1Player : MonoBehaviour
{
	public InputControlCenter inputCtrl;

	protected Animator animator;
	protected LV1CharaAction chAct;
	public event EventHandler<SingEventArg> evtPlayerSing;
	public int pitchLevel;
	/*public float upDistence;
	public int circleFrame;

	protected List<float> deltaPos;
	protected bool signSended;*/
	
	// Use this for initialization
	void Start ()
	{
		animator = GetComponent<Animator> ();
		chAct = GetComponent<LV1CharaAction> ();
		pitchLevel = 0;

		//deltaPos = new List<float> ();
		//signSended = false;

		inputCtrl.evtUserInput += new System.EventHandler<InputEventArgs> (OnInputEvent);
	}

	public void OnInputEvent(object send, InputEventArgs args)
	{
		if (args.Action == InputAction.PressDown){
			chAct.Shutup ();
		}

		if (args.Action == InputAction.RaiseUp) {
			chAct.Sing(pitchLevel);
		}

		if (args.Action == InputAction.SliderUp) {
			chAct.Shout();
		}
	}

	// Update is called once per frame
	void Update ()
	{
		/*if (Input.GetButtonDown ("Fire1")) {
			if(chAct.Shutup())
			{
				if (null != evtPlayerSing)
						evtPlayerSing (this, new SingEventArg (SingAction.shutup));
			}
		}
		
		if (Input.GetButtonUp ("Fire1")) {
			if(chAct.Sing(pitchLevel))
			{
				if (null != evtPlayerSing)
						evtPlayerSing (this, new SingEventArg (SingAction.sing));
			}
		}

		if(Input.GetButton("Fire1")){
			if(signSended)
				return;

			if(deltaPos.Count >=  circleFrame)
			{
				deltaPos.RemoveAt(0);
			}

			deltaPos.Add(Input.GetAxis("Mouse Y"));
			float totalPos = 0;
			foreach(float fDelta in deltaPos){
				totalPos += fDelta;
			}
			if(totalPos > upDistence){
				if(chAct.Shout())
				{
					signSended = true;
					if (null != evtPlayerSing)
						evtPlayerSing (this, new SingEventArg (SingAction.shoud));
				}
			}
		}
		else
		{
			deltaPos.Clear();
			signSended = false;
		}*/
	}
}

// 玩家最终的结果
public enum SingAction
{
    sing,
    shutup,
    shoud,
}

public class SingEventArg : EventArgs
{
    public SingEventArg(SingAction act)
	{
        action = act;
	}
    private SingAction action;

    public SingAction Singing
	{
        get{return action;}
        set{action = value;}
	}
}
